package shuaige.jdk.datetime;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

public class LocalDateTimeDemo {
    public static void main(String[] args) {
        转换系列_LocalDateTime转LocalDate();
    }

    public static void 添加系列() {
        LocalDateTime now = LocalDateTime.now();
        // 自定义添加-日
        now.plus(1, ChronoUnit.DAYS);
        // 添加日
        now.plusDays(1);
        // 添加周
        now.plusWeeks(1);
        // 添加月
        now.plusMonths(1);
        // 添加年
        now.plusYears(1);
    }

    public static void 转换系列_LocalDateTime转LocalDate() {
        // 直接调用 toLocalDate() 方法
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDate localDate = localDateTime.toLocalDate();
        System.out.println(localDate);
    }

    public static void 转换系列_LocalDateTime转换为Date() {
        LocalDateTime localDateTime = LocalDateTime.now();
        //获取系统默认时区
        ZoneId zoneId = ZoneId.systemDefault();
        //时区的日期和时间
        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
        //获取时刻
        Date date = Date.from(zonedDateTime.toInstant());
        System.out.println("Date:" + date);
    }

    public static void 转换系列_Date转LocalDateTime() {
        Date date = new Date();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = date.toInstant().atZone(zoneId).toLocalDateTime();
        System.out.println(localDateTime);
    }

    public static void 格式化() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter localDateTimeFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println("格式化:" + localDateTimeFormat.format(now));
    }


}
